package com.ems.mapper;

import com.ems.entity.AlarmEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ems.vo.AlarmVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 报警/故障 Mapper 接口
 * </p>
 * @author WuZH
 * @since 2023-10-24
 */
@Mapper
public interface AlarmMapper extends BaseMapper<AlarmEntity> {

    @Select (value = "<script>SELECT * FROM alarm_info ai\n" +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='alarmVo.stationId!=null'> AND ai.stationId=#{alarmVo.stationId} </if>" +
            "<if test='alarmVo.type!=null'> AND ai.type=#{alarmVo.type} </if>" +
            "ORDER BY ai.createTime DESC LIMIT #{alarmVo.page}, #{alarmVo.limit}</trim></script>")
    List<AlarmVo> queryList (@Param ("alarmVo") AlarmVo alarmVo);


    @Select (value = "<script>SELECT COUNT(1) FROM alarm_info ai\n" +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='alarmVo.stationId!=null'> AND ai.stationId=#{alarmVo.stationId} </if>" +
            "<if test='alarmVo.type!=null'> AND ai.type=#{alarmVo.type} </if>" +
            "</trim></script>")
    Integer queryCount (@Param ("alarmVo") AlarmVo alarmVo);


    @Select (value = "<script>SELECT *,si.stationName AS stationName FROM alarm_info ai LEFT JOIN station_info si ON ai.stationId=si.id WHERE ai.id=#{alarmVo.id}</script>")
    AlarmVo querySingle (@Param ("alarmVo") AlarmVo alarmVo);

    /**
     * 查询今天报警列表
     * @param alarmVo
     * @return
     */
    @Select (value = "<script>SELECT * FROM alarm_info ai " +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='alarmVo.stationId!=null'> AND ai.stationId=#{alarmVo.stationId} </if>\n" +
            "AND date_format(createTime, '%Y-%M-%D') = date_format(current_date(), '%Y-%M-%D')\n" +
            "</trim></script>")
    List<AlarmVo> queryTodayList (@Param ("alarmVo") AlarmVo alarmVo);

}
